<?php
namespace EasySwoole\EasySwoole;


use App\Lib\Tool\Constant;
use EasySwoole\Component\Di;
use EasySwoole\EasySwoole\Swoole\EventRegister;
use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\Http\Request;
use EasySwoole\Http\Response;
use EasySwoole\ORM\DbManager;
use EasySwoole\ORM\Db\Connection;
use EasySwoole\ORM\Db\Config;
use EasySwoole\EasySwoole\Config as GlobalConfig;
use EasySwoole\Session\Session;
use EasySwoole\Template\Render;
use EasySwoole\Http\GlobalParamHook;
use EasySwoole\Session\SessionFileHandler;
use EasySwoole\AtomicLimit\AtomicLimit;

class EasySwooleEvent implements Event
{

    public static function initialize()
    {
        // TODO: Implement initialize() method.
        date_default_timezone_set('Asia/Shanghai');
        //定义常量文件
        Constant::init();
        //定义路由层数
        Di::getInstance()->set(SysConst::HTTP_CONTROLLER_MAX_DEPTH,3);
        //数据库ORM
        self::mysql();

    }

    public static function mainServerCreate(EventRegister $register)
    {
        //渲染模板 配置
        $render = Render::getInstance();
        $render->getConfig()->setRender(new \App\Think());
        $render->attachServer(ServerManager::getInstance()->getSwooleServer());
        // TODO: Implement mainServerCreate() method.
        $handler = new SessionFileHandler(EASYSWOOLE_TEMP_DIR);
        GlobalParamHook::getInstance()->hookDefault();
        //如果不需要session请勿注册
        GlobalParamHook::getInstance()->hookSession($handler,'easy_session','session_dir');

        //热加载
        $hotReloadOptions = new \EasySwoole\HotReload\HotReloadOptions;
        $hotReload = new \EasySwoole\HotReload\HotReload($hotReloadOptions);
        $hotReloadOptions->setMonitorFolder([EASYSWOOLE_ROOT . '/App']);

        $server = ServerManager::getInstance()->getSwooleServer();
        $hotReload->attachToServer($server);

        //限流
        AtomicLimit::getInstance()->addItem('default')->setMax(200);
        AtomicLimit::getInstance()->addItem('api')->setMax(2);
        AtomicLimit::getInstance()->enableProcessAutoRestore(ServerManager::getInstance()->getSwooleServer(),10*1000);

    }

    public static function onRequest(Request $request, Response $response): bool
    {
        //session 配置
        $cookie = $request->getCookieParams('easy_session');
        if(empty($cookie)){
            $sid = Session::getInstance()->sessionId();
            $response->setCookie('easy_session',$sid);
        }else{
            Session::getInstance()->sessionId($cookie);
        }

        return true;
    }

    public static function afterRequest(Request $request, Response $response): void
    {
        // TODO: Implement afterAction() method.
    }

    //初始化mysql
    public static function  mysql(){

        $configItem = GlobalConfig::getInstance()->getConf("MYSQL");


        $config = new Config();
        $config->setDatabase($configItem['database']);
        $config->setUser($configItem['user']);
        $config->setPassword($configItem['password']);
        $config->setHost($configItem['host']);
        //连接池配置
        $config->setGetObjectTimeout(3.0); //设置获取连接池对象超时时间
        $config->setIntervalCheckTime(30*1000); //设置检测连接存活执行回收和创建的周期
        $config->setMaxIdleTime(15); //连接池对象最大闲置时间(秒)
        $config->setMaxObjectNum(20); //设置最大连接池存在连接对象数量
        $config->setMinObjectNum(5); //设置最小连接池存在连接对象数量
        DbManager::getInstance()->addConnection(new Connection($config),'wr');
    }
}